/**
 * All rights reserved by www.xmerge.cn
 */
package net.xfok.info.job;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author YangHua
 *
 */
public class InfoAdapter extends BaseAdapter {
	private final float TEXT_SIZE=20.0f;
	private List<InfoItem> infoItems;
	private LayoutInflater mInflater;
    private Bitmap itemIcon;
    private Context context;
    
	public InfoAdapter(Context context){
		mInflater = LayoutInflater.from(context);
		itemIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);
		infoItems=new ArrayList<InfoItem>();
		this.context=context;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getCount()
	 */
	public int getCount() {
		// TODO Auto-generated method stub
		return infoItems.size();
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItem(int)
	 */

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return infoItems.get(position);
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItemId(int)
	 */

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
	 */

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.list_item, null);
			holder = new ViewHolder();
			holder.text = (TextView) convertView.findViewById(R.id.text);
			holder.icon = (ImageView) convertView.findViewById(R.id.item_icon);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.text.setText(infoItems.get(position).getTitle()+" ["+infoItems.get(position).getDate()+"]");
		holder.text.setTextSize(TEXT_SIZE);
		holder.icon.setImageBitmap(itemIcon);
		return convertView;
    }
 	static class ViewHolder {
 		TextView text;
 		ImageView icon;
 	}
 	
 	public void add(InfoItem infoItem){
 		if(infoItem!=null){
 			infoItems.add(infoItem);
 		}
 	}
}
